package com.fang.study.config;

import com.fang.study.util.ZkUtils;
import com.fang.study.watch.WatchCallback;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @author fanglingxiao
 * @version 1.0
 * @description TODO
 * @date 2022/7/6 4:18 下午
 **/
public class TestZkConfig {

    private ZooKeeper zk;
    private static final String path = "/conf";

    @Before
    public void getConn() {
        zk = ZkUtils.getZk();
    }

    @After
    public void close() {
        try {
            zk.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test() {
        MyConf conf = new MyConf();
        WatchCallback callback = new WatchCallback(zk, path, conf);
        callback.aWait();
        // 1. 节点不存在
        // 2. 节点存在
        while (true) {
            if ("".equals(conf.getConf())) {
                System.out.println("节点数据丢失...");
                callback.aWait();
            } else {
                System.out.println(conf.getConf());
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
